package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseStudentController {

    private StudentService studentService = new StudentService();
    //键盘录入
   protected   Scanner sc = new Scanner(System.in);
    //定义方法 作为 学生系统的入口
    public final void start() {
        Scanner sc = new Scanner(System.in);
        //死循环
        loop:
        while (true) {

            //键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            //选择
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("新增");
                    addStudent();
                    break;
                case "2":
                    // System.out.println("删除");
                    deleteStudent();
                    break;
                case "3":
                    // System.out.println("修改");
                    updateStudent();

                    break;
                case "4":
                    //  System.out.println("查看");
                    findAllStudents();
                    break;
                case "5":
                    System.out.println("再见");
                    //break loop;
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }


    //定义方法  添加学生
    //参数
    //返回值
    public  final  void addStudent() {
        StudentService studentService = new StudentService();
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //判断学号是否存在
            boolean flag = studentService.isExist(sid);
            if (flag) {
                System.out.println("已经存在,请重新输入");
            } else {
                //要了
                break;
            }
        }
        Student student = luruXinxi(sid);
        //调用service

        this.studentService.addStudent(student);//如果使用集合 必然成功
        //打印
        System.out.println("添加成功");


    }



    //查看方法
    public  final  void findAllStudents() {
        StudentService studentService = new StudentService();
        //找service 要数据
        ArrayList<Student> list = studentService.findAllStudents();
        //获取到数据  遍历打印
        // 判断集合是否有元素
        if (list == null || list.size() == 0) {
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "岁\t" + student.getBirthday());
        }
    }

    //修改
    public  final  void updateStudent() {
        ArrayList<Student> list = studentService.findAllStudents();
        //获取到数据  遍历打印
        // 判断集合是否有元素
        if (list == null || list.size() == 0) {
            System.out.println("没有数据请先添加");
            return;
        }
        //录入学号
        String sid = luruXueHao();
        //修改 重新录入其他信息
        Student student = luruXinxi(sid);
        studentService.updateStudent(sid,student);
        System.out.println("修改成功");
    }


    //删除方法
    public   final void deleteStudent() {
        ArrayList<Student> list = studentService.findAllStudents();
        //获取到数据  遍历打印
        // 判断集合是否有元素
        if (list == null || list.size() == 0) {
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = luruXueHao();
        // //删除
        studentService.deleteStudent(sid);
        System.out.println("删除成功");

    }

    public  final  String luruXueHao() {
        //录入学号
        System.out.println("请输入学号");
        String sid;
        while (true) {
            sid = sc.next();
            // 判断是否存在
            boolean exist = studentService.isExist(sid);
            //存在 删除
            if (exist) {
                break;
            } else {//不存在 重新录
                System.out.println("录入的学号不存在,请重新输入");
            }
        }
        return sid;
    }
    //ctrl+alt+m
    public  abstract Student luruXinxi(String sid);
}
